package com.cw.demo.springbean;

import org.springframework.beans.BeansException;
import org.springframework.beans.PropertyValues;
import org.springframework.beans.factory.config.SmartInstantiationAwareBeanPostProcessor;

import lombok.extern.slf4j.Slf4j;

/**
 * TODO
 * extends InstantiationAwareBeanPostProcessorAdapter已废弃
 * @author chenw
 * @date 2021/1/8
 */
@Slf4j
public class MyInstantiationAwareBeanPostProcessor implements SmartInstantiationAwareBeanPostProcessor {
    public MyInstantiationAwareBeanPostProcessor() {
        super();
        log.info("这是SmartInstantiationAwareBeanPostProcessor构造器");
    }
    /** 接口方法、实例化Bean之前调用*/
    @Override
    public Object postProcessBeforeInstantiation(Class bean, String beanName) throws BeansException {
        log.info("接口方法、实例化Bean之前调用#InstantiationAwareBeanPostProcessor调用postProcessBeforeInstantiation方法");
        return null;
    }

    @Override
    public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
        log.info("postProcessAfterInstantiation会执行吗？");
        //如果返回为 false, 则会终止属性注入
        //spring这里啥也没干，所以可以直接跳过
        return true;
    }

    /** 接口方法、实例化Bean之后调用*/
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        log.info("接口方法、实例化Bean之后调用#InstantiationAwareBeanPostProcessor调用postProcessAfterInitialization方法");
        return bean;
    }
    /** 接口方法、设置某个属性时调用 */
    @Override
    public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName)
    throws BeansException {
        log.info("接口方法、设置某个属性时调用#InstantiationAwareBeanPostProcessor调用postProcessPropertyValues方法");
        return pvs;
    }
}
